ID, $query_object->post_type ) && $query_object instanceof WP_Post ) { $vis_object = self::get_wpml_canonical_post_for_visibility( $query_object ); } // Logged-in check if ( isset( $logic['general']['logged'] ) ) { if( ! is_user_logged_in() ) { return false; } unset( $logic['general']['logged'] ); if( empty( $logic['general'] ) ) { unset( $logic['general'] ); } } // User role check if ( ! empty( $logic['roles'] ) // check if *any* of user's role(s) matches && ! count( array_intersect( wp_get_current_user()->roles, array_keys( $logic['roles'], true ) ) ) ) { return false; // bail early. } unset( $logic['roles'] ); if ( ! empty( $logic ) ) { $post_type = get_post_type(); if ( ( isset($logic['general']['home']) && is_front_page()) || ( isset($logic['general']['blog']) && is_home()) || ( isset($logic['general']['404']) && ( is_404() || self::is_custom_404() ) ) || ( isset( $logic['general']['page'] ) && is_page() && ! is_front_page() && ! self::is_custom_404() ) || ( $post_type === 'post' && isset($logic['general']['single']) && is_single()) || ( isset($logic['general']['search']) && is_search() ) || ( isset($logic['general']['author']) && is_author()) || ( isset($logic['general']['category']) && is_category() ) || ( isset($logic['general']['tag']) && is_tag()) || ( isset($logic['general']['date']) && is_date() ) || ( isset($logic['general']['year']) && is_year()) || ( isset($logic['general']['month']) && is_month()) || ( isset($logic['general']['day']) && is_day()) || (isset($query_object) && (( $post_type !== 'page' && $post_type !== 'post' && isset($logic['general'][$post_type]) && is_singular() ) || ( isset( $query_object->name ) && $query_object->name !== 'page' && $query_object->name !== 'post' && isset( $logic['post_type_archive'][$query_object->name] ) && is_post_type_archive() ) || ( is_tax() && isset($logic['general'][$query_object->taxonomy])))) ) { return true; } else { // let's dig deeper into more specific visibility rules if ( ! empty( $logic['tax'] ) ) { if ( is_singular() ) { if( !empty($logic['tax']['category_single'])){ // Backward compatibility reset($logic['tax']['category_single']); $first_key = key($logic['tax']['category_single']); if(!is_array($logic['tax']['category_single'][$first_key])){ $logic['tax']['category_single'] = array('category'=> $logic['tax']['category_single']); } if ( empty( $logic['tax']['category_single']['category'] ) ) { $cat = get_the_category(); if(!empty($cat)){ foreach($cat as $c){ if($c->taxonomy === 'category' && isset($logic['tax']['category_single']['category'][$c->slug])){ return true; } } } unset($logic['tax']['category_single']['category']); } foreach ($logic['tax']['category_single'] as $key => $tax) { $terms = get_the_terms( get_the_ID(), $key); if ( $terms !== false && !is_wp_error($terms) && is_array($terms) ) { foreach ( $terms as $term ) { $term->slug = urldecode( $term->slug ); /* for non-ascii term slugs */ if( isset($logic['tax']['category_single'][$key][$term->slug]) ){ return true; } } } } } } else { foreach ( $logic['tax'] as $tax => $terms ) { $terms = array_keys( $terms ); self::update_non_ascii_slugs( $terms ); if ( ( $tax === 'category' && is_category($terms) ) || ( $tax === 'post_tag' && is_tag( $terms ) ) || ( is_tax( $tax, $terms ) ) ) { return true; } } } } if (! empty( $logic['post_type'] ) ) { foreach ( $logic['post_type'] as $post_type => $posts ) { $posts = array_keys( $posts ); self::update_non_ascii_slugs( $posts ); if ( // Post single (compare via default-language post when WPML is active, so one condition applies to all translations) ( $post_type === 'post' && is_single() && $vis_object instanceof WP_Post && in_array( $vis_object->post_name, $posts, true ) ) // Page view || ( $post_type === 'page' && ( ( $vis_object instanceof WP_Post && self::hook_visibility_page_matches( $posts, $vis_object ) ) || ( ! is_front_page() && is_home() && in_array( self::get_wpml_posts_page_slug_for_visibility(), $posts, true ) ) // Posts page || ( themify_is_shop() && in_array( self::get_wpml_shop_page_slug_for_visibility(), $posts, true ) ) // WC Shop page ) ) // Custom Post Types single view check || ( $vis_object instanceof WP_Post && isset( $vis_object->post_parent ) && (int) $vis_object->post_parent <= 0 && is_singular( $post_type ) && in_array( $vis_object->post_name, $posts, true ) ) || ( $vis_object instanceof WP_Post && isset( $vis_object->post_parent ) && (int) $vis_object->post_parent > 0 && is_singular( $post_type ) && in_array( '/' . self::child_post_name( $vis_object ) . '/', $posts, true ) ) // for all posts of a post type. || ( is_singular( $post_type ) && in_array( 'E_ALL', $posts, true ) ) ) { return true; } } } if ( themify_is_shop() && ( $s = self::get_wpml_shop_page_slug_for_visibility() ) && isset( $logic['post_type']['page'][ $s ] ) ) { return true; } if ( isset( $logic['wc'] ) && themify_is_woocommerce_active()) { foreach( array_keys( $logic['wc'] ) as $endpoint ) { if ( is_wc_endpoint_url( $endpoint ) ) { return true; } } } } } return false; } private static function is_custom_404() { return class_exists( 'Themify_Custom_404' ) && Themify_Custom_404::is_custom_404(); } /** * Resolve a page/post to its WPML default-language (original) post so stored visibility rules * match the slugs/paths saved when conditions were set on the source language. * * @param WP_Post $post Queried singular post. * @return WP_Post */ private static function get_wpml_canonical_post_for_visibility( WP_Post $post ) { if ( ! has_filter( 'wpml_object_id' ) ) { return $post; } $default_lang = apply_filters( 'wpml_default_language', null ); if ( ! is_string( $default_lang ) || $default_lang === '' ) { return $post; } $canonical_id = (int) apply_filters( 'wpml_object_id', (int) $post->ID, $post->post_type, true, $default_lang ); if ( $canonical_id < 1 || $canonical_id === (int) $post->ID ) { return $post; } $found = get_post( $canonical_id ); return ( $found instanceof WP_Post ) ? $found : $post; } /** * Match saved page conditions (slugs and nested paths) against the canonical (default language) post. * * @param string[] $posts Parsed condition keys. * @param WP_Post $vis Default-language post for the current view. */ private static function hook_visibility_page_matches( array $posts, WP_Post $vis ) { if ( (int) $vis->post_parent <= 0 ) { return in_array( $vis->post_name, $posts, true ); } $home = strtok( get_home_url(), '?' ); $rel = str_replace( $home, '', remove_query_arg( 'lang', get_permalink( $vis->ID ) ) ); return in_array( '/' . $rel, $posts, true ) || in_array( ltrim( $rel, '/' ), $posts, true ) || in_array( '/' . self::child_post_name( $vis ) . '/', $posts, true ); } /** * Post name of the "Posts" page in the default language (for WPML visibility keys). */ private static function get_wpml_posts_page_slug_for_visibility() { $pfor = (int) get_option( 'page_for_posts' ); if ( $pfor > 0 && has_filter( 'wpml_object_id' ) ) { $def = apply_filters( 'wpml_default_language', null ); if ( is_string( $def ) && $def !== '' ) { $pfor = (int) apply_filters( 'wpml_object_id', $pfor, 'page', true, $def ); } } return $pfor > 0 ? (string) get_post_field( 'post_name', $pfor ) : ''; } /** * Shop page slug in the default language (WooCommerce + WPML), for visibility keys. */ private static function get_wpml_shop_page_slug_for_visibility() { if ( ! themify_is_shop() ) { return ''; } $sid = (int) themify_shop_pageId(); if ( $sid < 1 ) { return ''; } if ( has_filter( 'wpml_object_id' ) ) { $def = apply_filters( 'wpml_default_language', null ); if ( is_string( $def ) && $def !== '' ) { $mapped = (int) apply_filters( 'wpml_object_id', $sid, 'page', true, $def ); if ( $mapped > 0 ) { $sid = $mapped; } } } return (string) get_post_field( 'post_name', $sid ); } private static function update_non_ascii_slugs(array &$array ) { if(function_exists('mb_check_encoding')){ foreach ( $array as &$value ) { if ( isset($value) && ! mb_check_encoding( $value, 'ASCII' ) ) { /* revert "/" character, needed to check for nested posts */ $value= str_replace( '%2f', '/', strtolower( urlencode( $value ) )); } } } } public static function output_item() { $hook = current_filter(); $pre = self::PRE; foreach ( self::$action_map[ $hook ] as $id ) { if ( ! empty( self::$data["{$pre}-{$id}-r"] ) ) { if ( ! isset( self::$counter[ $id ] ) ) { self::$counter[ $id ] = 0; } self::$counter[ $id ]++; $max = isset( self::$data["{$pre}-{$id}-m"] ) ? (int) self::$data["{$pre}-{$id}-m"] : 0; if ( self::$counter[ $id ] % (int) self::$data["{$pre}-{$id}-r"] !== 0 || ( $max !== 0 && self::$counter[ $id ] / (int) self::$data["{$pre}-{$id}-r"] > $max ) ) { /* skip showing the content of the hook */ continue; } } if ( ! empty( self::$data["{$pre}-{$id}-code"] ) ) { echo apply_filters( 'themify_hooks_item_content', self::themify_do_shortcode_wp( '' . self::$data["{$pre}-{$id}-code"] . '' ), __CLASS__ ); } } } public static function between_content_filter( $content ) { if ( get_the_ID() !== get_queried_object_id() ) { return $content; } $pre = self::PRE; $content_block = explode( '
', $content ); if ( isset( self::$action_map['tf_after_nth_p'] ) ) { foreach ( self::$action_map['tf_after_nth_p'] as $id ) { $n = ! empty( self::$data["{$pre}-{$id}-r"] ) ? (int) self::$data["{$pre}-{$id}-r"] : 1; if ( ! empty( $content_block[ $n ] ) ) { $content_block[ $n ] .= self::$data["{$pre}-{$id}-code"]; } } } $count=count( $content_block ); if ( isset( self::$action_map['tf_after_every_nth_p'] ) ) { foreach ( self::$action_map['tf_after_every_nth_p'] as $id ) { $n = ! empty( self::$data["{$pre}-{$id}-r"] ) ? (int) self::$data["{$pre}-{$id}-r"] : 1; for ( $i = 1; $i < $count; $i++ ) { if ( $i % $n === 0 ) { $content_block[ $i ] .= self::$data["{$pre}-{$id}-code"]; } } } } /* stich the content back together */ for ( $i = 1; $i < $count; $i++ ) { $content_block[ $i ] = '
' . $content_block[ $i ]; } return implode( '', $content_block ); } /** * Returns a list of available hooks for the current theme. * * @return mixed */ public static function get_locations() { return self::$hook_locations; } public static function register_location( $id, $label, $group = 'layout' ) { self::$hook_locations[$group][$id] = $label; } public static function unregister_location($id) { foreach ( self::$hook_locations as $group => $hooks ) { unset( self::$hook_locations[$group][$id] ); } } private static function get_location_groups() { return array( 'layout' => __( 'Layout', 'themify' ), 'general' => __( 'General', 'themify' ), 'post' => __( 'Post', 'themify' ), 'post_module' => __( 'Builder Post Module', 'themify' ), 'comments' => __( 'Comments', 'themify' ), 'ecommerce' => __( 'WooCommerce Pages', 'themify' ), 'wc_loop' => __( 'WooCommerce Products', 'themify' ), 'ptb' => __( 'Post Type Builder', 'themify' ), 'post_content' => __( 'In Between Post Content', 'themify' ), ); } public static function register_default_hook_locations() { foreach ( array( array( 'wp_head', 'wp_head', 'general' ), array( 'wp_footer', 'wp_footer', 'general' ), array( 'themify_body_start', 'body_start', 'layout' ), array( 'themify_header_before', 'header_before', 'layout' ), array( 'themify_header_start', 'header_start', 'layout' ), array( 'themify_header_end', 'header_end', 'layout' ), array( 'themify_header_after', 'header_after', 'layout' ), array( 'themify_mobile_menu_start', 'main_menu_wrapper_start', 'layout' ), array( 'themify_mobile_menu_end', 'main_menu_wrapper_end', 'layout' ), array( 'themify_layout_before', 'layout_before', 'layout' ), array( 'themify_content_before', 'content_before', 'layout' ), array( 'themify_content_start', 'content_start', 'layout' ), array( 'themify_post_before', 'post_before', 'post' ), array( 'themify_post_start', 'post_start', 'post' ), array( 'themify_before_post_image', 'before_post_image', 'post' ), array( 'themify_after_post_image', 'after_post_image', 'post' ), array( 'themify_before_post_title', 'before_post_title', 'post' ), array( 'themify_after_post_title', 'after_post_title', 'post' ), array( 'themify_before_post_content', 'before_post_content', 'post' ), array( 'themify_after_post_content', 'after_post_content', 'post' ), array( 'themify_post_end', 'post_end', 'post' ), array( 'themify_post_after', 'post_after', 'post' ), array( 'themify_post_before_module', 'builder_post_before', 'post_module' ), array( 'themify_post_start_module', 'builder_post_start', 'post_module' ), array( 'themify_before_post_image_module', 'builder_before_post_image', 'post_module' ), array( 'themify_after_post_image_module', 'builder_after_post_image', 'post_module' ), array( 'themify_before_post_title_module', 'builder_before_post_title', 'post_module' ), array( 'themify_after_post_title_module', 'builder_after_post_title', 'post_module' ), array( 'themify_post_end_module', 'builder_post_end', 'post_module' ), array( 'themify_post_after_module', 'builder_post_after', 'post_module' ), array( 'themify_comment_before', 'comments_before', 'comments' ), array( 'themify_comment_start', 'comments_start', 'comments' ), array( 'themify_comment_end', 'comments_end', 'comments' ), array( 'themify_comment_after', 'comments_after', 'comments' ), array( 'themify_content_end', 'content_end', 'layout' ), array( 'themify_content_after', 'content_after', 'layout' ), array( 'themify_sidebar_before', 'sidebar_before', 'layout' ), array( 'themify_sidebar_start', 'sidebar_start', 'layout' ), array( 'themify_sidebar_end', 'sidebar_end', 'layout' ), array( 'themify_sidebar_after', 'sidebar_after', 'layout' ), array( 'themify_layout_after', 'layout_after', 'layout' ), array( 'themify_footer_before', 'footer_before', 'layout' ), array( 'themify_footer_start', 'footer_start', 'layout' ), array( 'themify_footer_end', 'footer_end', 'layout' ), array( 'themify_footer_after', 'footer_after', 'layout' ), array( 'themify_body_end', 'body_end', 'layout' ), ) as $key => $value ) { self::register_location( $value[0], $value[1], $value[2] ); } /* register ecommerce hooks group only if current theme supports WooCommerce */ if ( themify_is_woocommerce_active() ) { foreach ( array( array( 'woocommerce_before_single_product', 'before_single_product', 'ecommerce' ), array( 'themify_product_image_start', 'product_image_start', 'wc_loop' ), array( 'themify_product_image_end', 'product_image_end', 'wc_loop' ), array( 'themify_product_title_start', 'product_title_start', 'wc_loop' ), array( 'themify_product_title_end', 'product_title_end', 'wc_loop' ), array( 'themify_product_price_start', 'product_price_start', 'wc_loop' ), array( 'themify_product_price_end', 'product_price_end', 'wc_loop' ), array( 'woocommerce_before_add_to_cart_form', 'before_add_to_cart_form', 'wc_loop' ), array( 'woocommerce_after_add_to_cart_form', 'after_add_to_cart_form', 'wc_loop' ), array( 'woocommerce_before_variations_form', 'before_variations_form', 'wc_loop' ), array( 'woocommerce_after_variations_form', 'after_variations_form', 'wc_loop' ), array( 'woocommerce_before_add_to_cart_button', 'before_add_to_cart_button', 'wc_loop' ), array( 'woocommerce_after_add_to_cart_button', 'after_add_to_cart_button', 'wc_loop' ), array( 'woocommerce_product_meta_start', 'product_meta_start', 'wc_loop' ), array( 'woocommerce_product_meta_end', 'product_meta_end', 'wc_loop' ), array( 'themify_before_product_tabs', 'before_product_tabs', 'ecommerce' ), array( 'themify_after_product_tabs', 'after_product_tabs', 'ecommerce' ), array( 'woocommerce_after_single_product', 'after_single_product', 'ecommerce' ), array( 'themify_checkout_start', 'checkout_start', 'ecommerce' ), array( 'themify_checkout_end', 'checkout_end', 'ecommerce' ), array( 'themify_ecommerce_sidebar_before', 'woocommerce_sidebar_before', 'ecommerce' ), array( 'themify_ecommerce_sidebar_after', 'woocommerce_sidebar_after', 'ecommerce' ), ) as $key => $value ) { self::register_location( $value[0], $value[1], $value[2] ); } } /* register hook locations for PTB plugin */ if ( class_exists( 'PTB',false ) ) { foreach ( array( array( 'ptb_before_author', 'before_author', 'ptb' ), array( 'ptb_after_author', 'after_author', 'ptb' ), array( 'ptb_before_category', 'before_category', 'ptb' ), array( 'ptb_after_category', 'after_category', 'ptb' ), array( 'ptb_before_comment_count', 'before_comment_count', 'ptb' ), array( 'ptb_after_comment_count', 'after_comment_count', 'ptb' ), array( 'ptb_before_comments', 'before_comments', 'ptb' ), array( 'ptb_after_comments', 'after_comments', 'ptb' ), array( 'ptb_before_custom_image', 'before_custom_image', 'ptb' ), array( 'ptb_after_custom_image', 'after_custom_image', 'ptb' ), array( 'ptb_before_custom_text', 'before_custom_text', 'ptb' ), array( 'ptb_after_custom_text', 'after_custom_text', 'ptb' ), array( 'ptb_before_date', 'before_date', 'ptb' ), array( 'ptb_after_date', 'after_date', 'ptb' ), array( 'ptb_before_editor', 'before_content', 'ptb' ), array( 'ptb_after_editor', 'after_content', 'ptb' ), array( 'ptb_before_excerpt', 'before_excerpt', 'ptb' ), array( 'ptb_after_excerpt', 'after_excerpt', 'ptb' ), array( 'ptb_before_permalink', 'before_permalink', 'ptb' ), array( 'ptb_after_permalink', 'after_permalink', 'ptb' ), array( 'ptb_before_post_tag', 'before_post_tag', 'ptb' ), array( 'ptb_after_post_tag', 'after_post_tag', 'ptb' ), array( 'ptb_before_taxonomies', 'before_taxonomies', 'ptb' ), array( 'ptb_after_taxonomies', 'after_taxonomies', 'ptb' ), array( 'ptb_before_thumbnail', 'before_thumbnail', 'ptb' ), array( 'ptb_after_thumbnail', 'after_thumbnail', 'ptb' ), array( 'ptb_before_title', 'before_title', 'ptb' ), array( 'ptb_after_title', 'after_title', 'ptb' ), ) as $key => $value ) { self::register_location( $value[0], $value[1], $value[2] ); } } self::register_location( 'tf_after_nth_p', __( 'After nth paragraph', 'themify' ), 'post_content' ); self::register_location( 'tf_after_every_nth_p', __( 'After every nth paragraph', 'themify' ), 'post_content' ); } public static function config_setup($themify_theme_config) { $themify_theme_config['panel']['settings']['tab']['hook-content'] = array( 'title' => __( 'Hook Content', 'themify' ), 'id' => 'hooks', 'custom-module' => array( array( 'title' => __( 'Hook Content', 'themify' ), 'function' => array( __CLASS__, 'config_view' ), ), ) ); return $themify_theme_config; } public static function config_view($data = array()) { $data = themify_get_data(); $pre=self::PRE; $field_ids_json = isset( $data["{$pre}_field_ids"] ) ? $data["{$pre}_field_ids"] : ''; unset($data); $field_ids = json_decode( $field_ids_json ); if ( ! is_array( $field_ids ) ) { $field_ids = array(); } $field_ids = array_values( array_filter( $field_ids ) ); $out = '
' . __( 'Add item', 'themify' ) . '
'; $out .= ''; return $out; } public static function ajax_add_button() { check_ajax_referer( 'tf_nonce', 'nonce' ); if ( isset( $_POST['field_id'] ) && current_user_can( 'manage_options' ) ) { // Sanitize request-only ID to prevent attribute injection in admin HTML (do not alter stored values). $raw_id = (string) $_POST['field_id']; $id = is_numeric( $raw_id ) ? absint( $raw_id ) : sanitize_key( $raw_id ); echo self::item_template( $id, true ); } die; } private static function item_template( $id, $new = false ) { $pre=self::PRE; $id_attr = esc_attr( $id ); $output = '' . esc_html__( 'Only use wp_header to insert HTML metadata (scripts, styles, or meta tags) in the HTML
area. To add content in the top/header area, please select body_start.', 'themify' ) . '' . __( 'On archive loops, output this hook content on every ', 'themify' ) . ' ' . __( 'Output this hook content on every ', 'themify' ) . ' ' . __( 'Show after ', 'themify' ) . ' ' . __( 'Show after every ', 'themify' ) . '' . ': ' . __( 'nth post; ', 'themify' ) . ' ' . __( 'nth comment;', 'themify' ) . ' ' . __( 'nth paragraph.', 'themify' ) . ' ' . __( 'nth paragraph.', 'themify' ) . '' . '' . __( 'Maximum of ', 'themify' ) . '' . __(' times.', 'themify' ) . '